package com.ooqiu.gaming.ui.main.thread.video;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.api.entities.Article;
import com.ooqiu.gaming.api.repository.InformationRepository;
import com.ooqiu.gaming.base.BaseFragment;
import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.db.Channel;
import com.ooqiu.gaming.ui.article_detail.VideoDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
@Route(path = VideoContentFragment.PATH)
public class VideoContentFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    public static final String PATH = "/fragment/video/content";
    @BindView(R.id.rv_video_content_list)
    RecyclerView mRvVideoContentList;
    @BindView(R.id.swiper)
    SwipeRefreshLayout mSwiper;
    Unbinder unbinder;

    InformationRepository mInformationRepository;
    GamingApi mGamingApi;

    int page = 1;

    @Autowired(name = Constants.ROUTER_KEY_CHANNEL)
    Channel mChannel;


    VideoContentAdapter mVideoContentAdapter;

    List<Article> mArticles = new ArrayList<>();


    public VideoContentFragment() {

    }


    @Override
    public void initInjector(View view) {
        unbinder = ButterKnife.bind(this, view);
        getRouter().inject(this);
        mGamingApi = getApplicationComponent().getGamingApi();
        mInformationRepository = new InformationRepository(getApplicationComponent().getAppContext(), mGamingApi);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_video_content;
    }

    @Override
    public void initUI(View view) {
    }

    @Override
    public void initData() {


    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mSwiper.setOnRefreshListener(this);
        mRvVideoContentList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvVideoContentList.setAdapter(mVideoContentAdapter = new VideoContentAdapter(mArticles));
        mVideoContentAdapter.setEnableLoadMore(true);
        mVideoContentAdapter.setOnLoadMoreListener(this, mRvVideoContentList);

        mVideoContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Article article = (Article) adapter.getItem(position);
                getRouter().build(VideoDetailActivity.PATH)
                        .withInt("news_id", article.getNews_id())
                        .navigation();
            }
        });
        onRefresh();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void fetchVideoList(final boolean isRefresh) {
        mInformationRepository.fetchVideoList(page, Integer.parseInt(mChannel.getReal_id()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<Article>>(mContext) {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onNext(List<Article> articles) {
                        if (isRefresh) {
                            mArticles.clear();
                        }

                        if (articles.isEmpty()) {
                            mVideoContentAdapter.loadMoreEnd(true);
                        } else {
                            mVideoContentAdapter.loadMoreComplete();
                        }

                        mArticles.addAll(articles);
                        page++;
                        mVideoContentAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        mSwiper.setRefreshing(false);
                    }
                });
    }

    @Override
    public void onRefresh() {
        page = 1;
        mSwiper.setRefreshing(true);
        fetchVideoList(true);
    }

    @Override
    public void onLoadMoreRequested() {
        fetchVideoList(false);
    }
}
